<template>
    <div style="font-size: 16px;">
        <van-row style="text-align: center;">
            <van-col span="6">序号</van-col>
            <van-col span="6">获奖时间</van-col>
            <van-col span="6">奖品名称</van-col>
            <van-col span="6">状态</van-col>
        </van-row>
        <van-list v-model:loading="loading" :finished="finished" finished-text="">
            <van-row align="center" style="text-align: center;margin-top: 10px;"
            v-for="(item, index) in winList" :key="index" @click="getAwards(item)">
            <van-col span="6">{{ index + 1 }}</van-col>
            <van-col span="6">{{ parseTime(item.awardsTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</van-col>
            <van-col span="6">{{ item.awardName }}</van-col>
            <van-col span="6">{{ item.status }}</van-col>
        </van-row>
        </van-list>
        <div style="height: 100px;width: 100%;"></div>
    </div>
</template>

<script setup>
import { parseTime } from "@/api/utils/ruoyi.js"
import { ref, computed, onMounted } from 'vue';
import { showSuccessToast, showFailToast, showDialog } from 'vant';
import { getAwardsInfoByTaskIdAndOpenId } from "@/api/mine.js"
import { getUserTaskAwards } from "@/api/task.js"
import { useRoute, useRouter } from 'vue-router';

const winList = ref([]) //{no:'1',date:"2024-03-24 09:00:01",name:"红包",status:"已领取"}
const loading = ref(false)
const finished = ref(true)
const router = useRouter();

onMounted(() => {
    getList()
})

function getList() {
    let taskId = localStorage.getItem('taskId')
    let openId = localStorage.getItem('token')
    getAwardsInfoByTaskIdAndOpenId(taskId, openId).then(res => {
        if (res.code == 200) {
            winList.value = res.data
        } else {
            winList.value = []
        }
    })
}

function getAwards(dataItem) {
    var that = this
    if (dataItem.status != "未领取") {
        return false
    }
    const data = {};
    data.taskId = localStorage.getItem('taskId')
    data.longitude = localStorage.getItem('longitude');
    data.latitude = localStorage.getItem('latitude');
    data.preview = true;
    data.taskUserId = dataItem.id;
    data.memberId = localStorage.getItem('token');

    localStorage.setItem('taskUserId', dataItem.id)

    try {
        let url = `http://${location.host}/getAward`;
        location.href = `https://www.yaoyaola.net/exapi/check_user/10810602?url=${url}&flag=0`
    } catch (err) {
        showDialog({
            title: '关闭',
            message: err.message,
            confirmButtonText: '确定'
        }).then(() => {
            that.getList()
        });
    }


    // getUserTaskAwards(data).then(res => {
    //     console.log(res)
    //     var that = this
    //     if (res.code == 200) {
    //         if (res.data.awardsLevel) {
    //             // 跳转中奖页面
    //             // this.$router.push('/redPacket?userTaskId=' + res.data.id)

    //             showDialog({
    //                 title: '',
    //                 message: `恭喜您中了${res.data.awardsLevel || ''}，奖品是：${res.data.awardName || ''}`,
    //                 confirmButtonText: '立即领奖'
    //             }).then(() => {
    //                 // this.$router.push('/redPacket?userTaskId=' + res.data.id)
    //                 // this.showPhoneDialog = true;
    //                 let url = `http://${location.host}/getAward`;
    //                 console.log(url, 'url');
    //                 location.href = `https://www.yaoyaola.net/exapi/check_user/10810602?url=${url}&flag=0`
    //             });

    //         } else {
    //             // 跳转未中奖页面

    //             showDialog({
    //                 title: '',
    //                 message: '很遗憾，没有中奖',
    //                 confirmButtonText: '关闭'
    //             }).then(() => {
    //                 that.getList()
    //             });
    //         }
    //     }
    // }).catch((err) => {
    //     console.log(err);
    //     showDialog({
    //         title: '关闭',
    //         message: err.message,
    //         confirmButtonText: '确定'
    //     }).then(() => {
    //         that.getList()
    //     });
    // })
}
</script>
<style lang="less" scoped></style>